package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppCzMoneyService;
import com.vf.admin.client.service.AppOrderService;
import com.vf.admin.client.service.AppSetupService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.miaodong.entity.AppCzMoney;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.miaodong.entity.AppUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppCzController.java
 * @Description 充值金额
 * @createTime 2021年07月21日 10:58:0
 */
@CrossOrigin
@Controller
@RequestMapping("/app/chongzhi")
public class AppCzController extends ClientBaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    AppUserService appUserService;
    @Autowired
    AppSetupService appSetupService;
    @Autowired
    AppCzMoneyService appCzMoneyService;
    @Autowired
    AppOrderService appOrderService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * @return com.vf.admin.common.entity.Result
     * @author anxiang
     * @Description 获取钻石充值金额列表
     */
    @RequestMapping(value = "/getRechargeAmountList", method = RequestMethod.POST)
    @ResponseBody
    public Result getRechargeAmountList() {
        try {
            //获取当前登录用户的user对象
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            Map<String, Object> map = new HashMap<>(16);
            //获取钻石充值金额列表
            List<AppCzMoney> czMoneyList = appCzMoneyService.findList();
            //获取充值比例
            AppSetup setup = appSetupService.get();
            map.put("list", czMoneyList);
            map.put("diamonds", user.getDiamonds());
            map.put("rechargeRatio", setup.getRechargeratio());
            boolean firstRecharge = appOrderService.isFirstRecharge(userId);
            map.put("firstRecharge", firstRecharge ? 0 : 1);
            return this.sucessJSON(map);
        } catch (Exception e) {
            logger.error("获取钻石充值金额列表异常:", e);
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }


}
